/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.dejax.repository;

import com.dejax.domain.DevopsOperation;
import com.dejax.service.dto.DevopsOperationQueryCriteria;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* @website https://el-admin.vip
* @author Richer
* @date 2022-01-06
**/
public interface DevopsOperationRepository extends JpaRepository<DevopsOperation, String>, JpaSpecificationExecutor<DevopsOperation> {
    //同一个操作类型 同一个jenkinsid job_namespace 不能重复
    @Query(value = "select * from devops_operation where job_namespace = ?1 and jenkinsid = ?2 and flag = 0 ",nativeQuery = true)
    List<DevopsOperation> findOne(String job_namespace, String jenkinsid, String operate_type);


    @Query(value = "select devops_operation.*,jenkinsclient.ip from devops_operation " +
            "left join devops_jenkinsclient jenkinsclient on jenkinsclient.jenkinsid=devops_operation.jenkinsid  WHERE " +
            " IF( :#{#param.jenkinsid} IS NOT null, jenkinsclient.ip= :#{#param.jenkinsid},  1=1) "
            + "AND devops_operation.flag = 0  "
            + "ORDER BY ?#{#pageable}",
            countQuery = "SELECT count(*) FROM devops_operation" +
                    "left join devops_jenkinsclient jenkinsclient on jenkinsclient.jenkinsid=devops_operation.jenkinsid  WHERE " +
                    " IF( :#{#param.jenkinsid} IS NOT null, jenkinsclient.ip= :#{#param.jenkinsid},  1=1) "
                    + "AND devops_operation.flag = 0 ",
            nativeQuery=true)
    Page<DevopsOperation> getList(DevopsOperationQueryCriteria param, Pageable pageable);

}